﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using HousingInfo.Library;

namespace Admin.Admin
{
    public partial class Areas : System.Web.UI.Page
    {

        private static AreaBO areabo;
        private AreaDetailBO areadetailbo;
        protected void Page_Load(object sender, EventArgs e)
        {

            //if ((Session["LOGIN"] == null) || (int)Session["ROLES"] != 1)
            //{
            //    Response.Redirect("Login.aspx");
            //}
            string mode = Request.QueryString["Mode"];
            //Mode Add
            if (mode == "1")
            {
                if (!IsPostBack)
                {
                    areabo = AreaBO.NewAreaBO();
                }
                btnUpdate.Visible = false;
                btnDel.Visible = false;
                txtareaid.Visible = false;
                lbareaid.Visible = false;
                PanelActions.Visible = true;
                lbHeaderActions.Text = "Thêm khu vực";
                GridView1.Columns[0].Visible = false;

            }


            else //Mode Update
            {
                if (!IsPostBack)
                {

                    btnAdd.Visible = false;
                    txtareaid.Visible = true;
                    txtareaid.Enabled = false;
                    lbareaid.Visible = true;
                    PanelActions.Visible = false;
                    lbHeaderActions.Text = "Khu vực";
                }
            }

        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            CheckBox checkbox = new CheckBox();
            int areaid = Convert.ToInt32(GridView1.SelectedRow.Cells[1].Text);
            areabo = AreaBO.GetAreaBO(areaid);
            txtareaid.Text = areabo.AreaID.ToString();
            txtareaname.Text = areabo.AreaName;
            checkbox = (CheckBox)GridView1.SelectedRow.FindControl("cbIsActive");
            isActive.Checked = checkbox.Checked;
            listAreaDetail.Items.Clear();
            if (areabo.AreaDetailBOList.Count > 0)
            {
                listAreaDetail.DataSource = areabo.AreaDetailBOList;
                listAreaDetail.DataBind();
                listAreaDetail.SelectedIndex = 0;

            }
            if (PanelActions.Visible == false) PanelActions.Visible = true;
            PanelAreaDetail.Visible = false;
            messagedelete.Text = "";

        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            int areaid = Convert.ToInt32(txtareaid.Text);
            areabo.AreaName = txtareaname.Text;
            areabo.IsActive = isActive.Checked;
            try
            {
                areabo.Save();
                GridView1.DataBind();

                message.Text = "Cập nhật thành công !";
                GridView1_SelectedIndexChanged(null, null);
            }
            catch
            {
                message.Text = "Cập nhật thất bại !";
            }

        }

        protected void btnDel_Click(object sender, EventArgs e)
        {


            int areaid = Convert.ToInt32(txtareaid.Text);
            if (AreaDetailBOList.CheckAreaDetailinArea(areaid))
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Success", "alert('Bạn  phải xóa các khu vực con của khu vực cần xóa!')", true);
                return;
            }
            try
            {
                AreaBO.DeleteAreaBO(Convert.ToInt32(txtareaid.Text));
                GridView1.DataBind();
                messagedelete.Text = "Xóa thành công !";
                PanelActions.Visible = false;
                PanelAreaDetail.Visible = false;
            }
            catch
            {
                message.Text = "Xóa thất bại !";
            }
        }


        protected void btnAdd_Click(object sender, EventArgs e)
        {
            areabo.AreaName = txtareaname.Text;
            areabo.IsActive = isActive.Checked;
            try
            {
                areabo.Save();
                message.Text = "Thêm thành công !";
                GridView1.DataBind();
                txtareaname.Text = "";
                isActive.Checked = isActive.Checked;
            }
            catch
            {
                message.Text = "Thêm thất bại !";
            }

            
        }

        protected void btnImageAddAreaDetail_Click(object sender, ImageClickEventArgs e)
        {
            message.Text = "";
            lbAreaDetail.Text = "Thêm khu vực chi tiết";
            txtAreaDetailName.Text = "";
            isActiveAreaDetail.Checked = true;
            btnAddDetail.Visible = true;
            PanelAreaDetail.Visible = true;
            lbAreaDetailID.Visible = false;
            txtAreaDetailID.Visible = false;
            btnUpdateDetail.Visible = false;
        }

        protected void btnImageEditAreaDetail_Click(object sender, ImageClickEventArgs e)
        {
            message.Text = "";
            lbAreaDetail.Text = "Sửa khu vực chi tiết";
            PanelAreaDetail.Visible = true;
            btnAddDetail.Visible = false;
            btnUpdateDetail.Visible = true;
            areadetailbo = areabo.AreaDetailBOList.Single(a => a.AreaDetailID == Convert.ToInt32(listAreaDetail.SelectedItem.Value));
            txtAreaDetailID.Text = areadetailbo.AreaDetailID.ToString();
            txtAreaDetailName.Text = areadetailbo.AreaDetailName;
            isActiveAreaDetail.Checked = areadetailbo.IsActive;
        }



        protected void btnUpdateDetail_Click(object sender, EventArgs e)
        {
            areadetailbo = areabo.AreaDetailBOList.Single(a => a.AreaDetailID == Convert.ToInt32(listAreaDetail.SelectedItem.Value));
            areadetailbo.AreaDetailName = txtAreaDetailName.Text;    
            areadetailbo.IsActive = isActiveAreaDetail.Checked;
            PanelAreaDetail.Visible = false;
            if (areabo.AreaDetailBOList.Count > 0)
            {
                listAreaDetail.DataSource = areabo.AreaDetailBOList;
                listAreaDetail.DataBind();
                listAreaDetail.SelectedIndex = 0;

            }
        }

        protected void btnAddDetail_Click(object sender, EventArgs e)
        {

            AreaDetailBO areadetailbo = areabo.AreaDetailBOList.AddNew();
            //areadetailbo.AreaDetailID = _ID;
            //_ID--;
            areadetailbo.AreaDetailName = txtAreaDetailName.Text;
            areadetailbo.IsActive = isActiveAreaDetail.Checked;
            listAreaDetail.Items.Clear();
            listAreaDetail.DataSource = areabo.AreaDetailBOList;
            listAreaDetail.DataBind();
            PanelAreaDetail.Visible = false;

        }

        protected void btnImageDelAreaDetail_Click1(object sender, ImageClickEventArgs e)
        {
            message.Text = "";
            areadetailbo = areabo.AreaDetailBOList.Single(a => a.AreaDetailID == Convert.ToInt32(listAreaDetail.SelectedItem.Value));
            //dung khi lam xong phan tin tuc
            if (NewsHouseBOList.CheckNewsHouseinAreaDetail(areadetailbo.AreaDetailID, "", null))
            {
                ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Success", "alert('Bạn phải xóa những tin thuộc khu vực này trước !!!')", true);
                return;
            }
            listAreaDetail.Items.Remove(listAreaDetail.SelectedItem);
            areabo.AreaDetailBOList.Remove(areadetailbo);
        }
    }
}
